home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / InsideBa1994 / InsideBasic-94 / IB 94 / Apple Events / AEClient.bas next >
BASIC Source File  |  1993-08-30  |  27KB  |  743 lines

  1. WINDOW OFF : COORDINATE WINDOW
  2.  
  3. RESOURCES "AEClient.res","APPLAECL"
  4.  
  5. COMPILE 0,140
  6.  
  7. '=================================================
  8. '       AEClient
  9. '       Michael W. Faulkner 
  10. '       Inveterate Insomniac, Inc.
  11. '       Began   6/10/93   02:25  GMT
  12. '       Last Modified:       8/29/93
  13. '
  14. '       For brevity sake, I'll only comment
  15. '       the functions that are important for
  16. '       this example program
  17. '=================================================
  18.  
  19.  
  20.  
  21. '=================================================
  22. '       Constants for Apple Events
  23. '       The following is an incomplete list of
  24. '       constants required for Apple events. Look
  25. '       in Inside Mac for the complete list.
  26. '=================================================
  27. _typeLongInteger        = _"long"
  28. _errAEDescNotFound      = -1701
  29. _errAEEventNotHandled   = -1708
  30. _keyErrorNumber         = _"errn"
  31.  
  32. _kCoreEventClass        = _"aevt"
  33. _kAEOpenApplication     = _"oapp"
  34. _kAEOpenDocuments       = _"odoc"
  35. _kAEPrintDocuments      = _"pdoc"
  36. _kAEQuitApplication     = _"quit"
  37. _typeAEList             = _"list"
  38. _typeFSS                = _"fss "
  39. _keyDirectObject        = _"----"
  40. _typeWildCard           = _"****"
  41.  
  42. _kAENoReply             =       1
  43. _kAEQueueReply          =       2
  44. _kAEWaitReply           =       3
  45. _kAENeverInteract       =       16
  46. _kAECanInteract         =       32
  47. _kAEAlwaysInteract      =       48
  48. _kAECanSwitchLayer      =       64
  49. _kAEDontReconnect       =       128
  50. _kAutoGenerateReturnID  =       -1
  51. _kTransactionID         =       1
  52. _kAnyTransactionID      =       0
  53. _kAEDefaultTimeout      =       -1
  54. _kNoTimeout             =       -2
  55. _kAENormalPriority      =       0
  56. _nil                    =       &00000000
  57. _sizeOfSign             =       &00000004
  58.  
  59. _typeApplSignature      =       _"sign"
  60.  
  61.  
  62. _typenull               =       _"null"
  63.  
  64.  
  65. '**********************************************
  66. '*
  67. '*      IDs of the menu in the PopUp CDEF control
  68. '*      and menubar
  69. '**********************************************
  70.  
  71. _kAEOpenApp             =       1
  72. _kAEOpenDoc             =       2
  73. _kAEPrintDoc            =       3
  74. _kAEQuitApp             =       4
  75. _kAESendMessage         =       6
  76.  
  77. _fileMenu       = 1
  78. _senderItem     = 1
  79. _quitItem       = 2
  80.  
  81. _AEPopup        = 1
  82. _sendBtn        = 2
  83.  
  84.  
  85. '=======================================
  86. '
  87. '       Misc constants
  88. '
  89. '========================================
  90.  
  91. _kAEBarfAlert    = 129                    'Called if AE couldn't be handled
  92. _kScareUserAlert = 128                    'Tell user shuting down server
  93. _kPopup          = 1                      'PopUPID
  94. _kMainWindow     = 1                      'main window
  95. _kEditField      = 1
  96. _CDEF            = 128                    'resource ID of PopUp
  97.  
  98. '***************************************
  99. '*
  100. '*      For our Apple event, we need the
  101. '*      signatures of the client and server 
  102. '*      applications, and a custom event class
  103. '*      and event ID constants.
  104. '*
  105. '***************************************
  106.  
  107. _AECL            = _"AECL"                'signature of client
  108. _AESV            = _"AESV"                'signature of server
  109.  
  110. _AECustomClass   = _"cstm"                'Custom class
  111. _AECustomEventID = _"mssg"                'Custom ID
  112.  
  113. '***************************************
  114. '*
  115. '*      Global Varibles
  116. '*
  117. '***************************************
  118.  
  119. DIM gDone                                 'Set _true when done
  120.  
  121.  
  122. '*      See Inside Mac for complete descriptions
  123. '*      of the following records:
  124. '*      (My name ain't 'Xerox'....)
  125.  
  126. DIM RECORD AEDesc                         'AEDescriptor Record
  127.   DIM descType&
  128.   DIM descHandle&
  129. DIM END RECORD _AEDesc
  130.  
  131. DIM RECORD AEKeyDesc                      'AEKeyDescriptor Record
  132.   DIM descKey&
  133.   DIM descContent.AEDesc
  134. DIM END RECORD _AEKeyDesc
  135.  
  136. DIM RECORD AEAddressDesc                  'AEAddressDescriptor Record
  137.   DIM addressDesc.AEDesc
  138. DIM END RECORD _AEAddressDesc
  139.  
  140. DIM RECORD AEDescList                     'AEDescriptorList Record
  141.   DIM descList.AEDesc
  142. DIM END RECORD _AEDescList
  143.  
  144. DIM RECORD AERecord                       'AERecord Record
  145.   DIM recordDesList.AEDescList
  146. DIM END RECORD _AERecord
  147.  
  148. DIM RECORD AppleEvent                     'Apple Event Record
  149.   DIM AEEventRecord.AERecord
  150. DIM END RECORD _AppleEvent
  151.  
  152. DIM newAppleEvent.appleEvent              'Our new apple event of type AppleEvent
  153. DIM replyAppleEvent.appleEvent            'AppleEvent for reply from target
  154. DIM newAEDesc.AEDesc                      'The new AEDesc record
  155. DIM appSignature&                         'For bytes for target's signature
  156. DIM evtType&
  157.  
  158. appSignature& = _AESV                     'Signature of the server
  159.  
  160. END GLOBALS
  161.  
  162. '**************************************************************
  163.  
  164. GOTO "main"                               'Just Do It
  165.  
  166.  
  167. '**************************************************************
  168. '*
  169. '*      FN AECreateDesc(typeCode&,dataPtr&,dataSize&,result&)
  170. '*
  171. '*      Creates a new AEDesc Record for the AppleEvent
  172. '*      we need to build.
  173. '*
  174. '*      Selector: #$0825
  175. '*      Trap:     $A816 (_pack8)
  176. '*
  177. '*      Local variables: none
  178. '*
  179. '*      On entry:
  180. '*       typeCode& - long: type descriptor for AEDesc record
  181. '*       dataPtr&  - long: pointer to data for record
  182. '*       dataSize& - long: size of data in bytes
  183. '*       result&   - long: pointer to uninitialized AEDesc record
  184. '*
  185. '*      On exit:
  186. '*       result result passed in D0
  187. '**************************************************************
  188. LOCAL MODE
  189. LOCAL FN AECreateDesc(typeCode&,dataPtr&,dataSize&,result&)
  190.   `     subq.l  #2,sp
  191.   `     move.l  ^typeCode&,-(sp)
  192.   `     move.l  ^dataPtr&,-(sp)
  193.   `     move.l  ^dataSize&,-(sp)
  194.   `     move.l  ^result&,-(sp)
  195.   `     move.w  #$0825,D0
  196.   `     dc.w    $A816
  197.   `     move.w  (sp)+,D0
  198.   `     ext.l   D0
  199. END FN
  200.  
  201.  
  202. '**************************************************************
  203. '*
  204. '*      FN AECreateAppleEvent(evtClass&,evtID&,desc&,returnID%,transID&,result&)
  205. '*
  206. '*      Creates new AppleEvent from AEDesc record
  207. '*
  208. '*      Selector: #$0B14
  209. '*      Trap:     $A816 (_pack8)
  210. '*      Local variables: none
  211. '*
  212. '*      On entry:
  213. '*       evtClass& - long: event class of the new AppleEvent
  214. '*       evtID&    - long: event ID of new AppleEvent
  215. '*       desc&     - long: pointer to AEDesc record
  216. '*       returnID% - integer: returnID generation flag
  217. '*       transID&  - long: transaction ID
  218. '*       result&   - long: pointer to record of type AppleEvent
  219. '*
  220. '*      On exit:
  221. '*       result passed in D0
  222. '**************************************************************
  223. LOCAL MODE
  224. LOCAL FN AECreateAppleEvent(evtClass&,evtID&,descRecord&,returnID%,transID&,result&)
  225.   `     subq.l  #2,sp
  226.   `     move.l  ^evtClass&,-(sp)
  227.   `     move.l  ^evtID&,-(sp)
  228.   `     move.l  ^descRecord&,-(sp)
  229.   `     move.w  ^returnID%,-(sp)
  230.   `     move.l  ^transID&,-(sp)
  231.   `     move.l  ^result&,-(sp)
  232.   `     move.w  #$0B14,D0
  233.   `     dc.w    $A816
  234.   `     move.w  (sp)+,D0
  235.   `     ext.l   D0
  236. END FN
  237.  
  238. '**************************************************************
  239. '*
  240. '*      FN AESend(aevent&,aereply&,interaction&,priority&,timeout%,idleProc&,filterProc&)
  241. '*
  242. '*      Sends the new AppleEvent on its merry way
  243. '*
  244. '*      Selector: #$0D17
  245. '*      Trap:     $A816 (_pack8)
  246. '*      Local variables: none
  247. '*
  248. '*      On entry:
  249. '*       aevent&      - long: pointer to AppleEvent
  250. '*       aereply&     - long: pointer to AppleEvent reply record
  251. '*       interaction& - long: level of user interaction
  252. '*       priority&    - long: priority of AppleEvent
  253. '*       timeout%     - integer: timeout in ticks (-1 = 60 secs)
  254. '*       idleProc&    - long: pointer to sys idle procedure (valid only if no timeout specified)
  255. '*       filterProc&  - long: pointer to AEFilter procedure (valid only if no timeout specified)
  256. '*     
  257. '*       On exit:
  258. '*        result passed in D0
  259. '**************************************************************
  260. LOCAL MODE
  261. LOCAL FN AESend(aevent&,aereply&,interaction&,priority&,timeout%,idleProc&,filterProc&)
  262.   `     subq.l  #2,sp
  263.   `     move.l  ^aevent&,-(sp)
  264.   `     move.l  ^aereply&,-(sp)
  265.   `     move.l  ^interaction&,-(sp)
  266.   `     move.l  ^priority&,-(sp)
  267.   `     move.w  ^timeout%,-(sp)
  268.   `     move.l  ^idleProc&,-(sp)
  269.   `     move.l  ^filterProc&,-(sp)
  270.   `     move.w  #$0D17,D0
  271.   `     dc.w    $A816
  272.   `     move.w  (sp)+,D0
  273.   `     ext.l   D0
  274. END FN
  275.  
  276. '******************************************************************
  277. '*
  278. '*      FN AEPutParamPtr(newEvent&,keyword&,desc&,ptr&,dataSize&)
  279. '*
  280. '*      Attaches a parameter to a valid Apple event
  281. '*
  282. '*      Selector:       #$0A0F
  283. '*      Trap:           $A816
  284. '*
  285. '*      On Entry:
  286. '*              newEvent& : long -- pointer to Apple event
  287. '*              keyWord&  : long -- keyword which describes parameter
  288. '*              desc&     : long -- type descriptor for parameter
  289. '*              ptr&      : long -- pointer to data
  290. '*              dataSize& : long -- size of data pointed to
  291. '*
  292. '*      On exit:
  293. '*              result passed in D0
  294. '******************************************************************
  295. LOCAL MODE
  296. LOCAL FN AEPutParamPtr(newEvent&,keyword&,desc&,ptr&,dataSize&)
  297.   `     subq.l  #2,sp
  298.   `     move.l  ^newEvent&,-(sp)
  299.   `     move.l  ^keyword&,-(sp)
  300.   `     move.l  ^desc&,-(sp)
  301.   `     move.l  ^ptr&,-(sp)
  302.   `     move.l  ^dataSize&,-(sp)
  303.   `     move.w  #$0A0F,D0
  304.   `     dc.w    $A816
  305.   `     move.w  (sp)+,D0
  306.   `     ext.l   D0
  307. END FN
  308.  
  309. '******************************************************************
  310. '*
  311. '*      FN AEGetParamPtr(theEvent&,keyWord&,type&,resultType&,buffer&,maxSize&,actualSize&)
  312. '*
  313. '*      Given an Apple event, extracts a parameter into the buffer provided
  314. '*
  315. '*      Selector:       #$0E11
  316. '*      Trap:           $A816
  317. '*
  318. '*      On entry:
  319. '*              theEvent& : long -- pointer to the Apple event
  320. '*              keyWord&  : long -- keyword describing parameter
  321. '*              type&     : long -- type descriptor for parameter
  322. '*              resultType& : long -- pointer to buffer for return type
  323. '*              buffer&   : long -- pointer to buffer for parameter
  324. '*              maxSize&  : long -- maximum size for buffer
  325. '*              actualSize& : long -- pointer to buffer for actual size
  326. '*
  327. '*      On exit:
  328. '*              result returned in D0
  329. '******************************************************************
  330. LOCAL MODE
  331. LOCAL FN AEGetParamPtr(theEvent&,keyWord&,type&,resultType&,buffer&,maxSize&,actualSize&)
  332.   ` subq.l      #$2,sp
  333.   ` move.l      ^theEvent&,-(sp)
  334.   ` move.l      ^keyWord&,-(sp)
  335.   ` move.l      ^type&,-(sp)
  336.   ` move.l      ^resultType&,-(sp)
  337.   ` move.l      ^buffer&,-(sp)
  338.   ` move.l      ^maxSize&,-(sp)
  339.   ` move.l      ^actualSize&,-(sp)
  340.   ` move.w      #$0E11,D0
  341.   ` dc.w        $A816
  342.   ` move.w      (sp)+,D0
  343.   ` ext.l       D0     
  344. END FN
  345.  
  346. '******************************************************************
  347. '*
  348. '*      FN AEGetAttributePtr(theAEvent&,keyWord&,desiredType&,returnType&,bufferPtr&,bufferSize&,actualSize&)
  349. '*
  350. '*      Given an Apple event, extracts an attribute and places it in
  351. '*      the buffer provided.
  352. '*
  353. '*      Selector:       #$0E15
  354. '*      Trap:           $A816
  355. '*
  356. '*      On entry:
  357. '*              theAEvent&   : long -- pointer to Apple event
  358. '*              keyWord&     : long -- keyword of the attribute 
  359. '*              desiredType& : long -- the type descriptor the attribute is to be returned as
  360. '*              returnType&  : long -- the returned type descriptor of the attribute
  361. '*              bufferPtr&   : long -- pointer to buffer for the returned attribute
  362. '*              bufferSize&  : long -- size of the buffer
  363. '*              actualSize&  : long -- buffer for size to be returned to
  364. '*
  365. '*      On exit:
  366. '*              result returned in D0
  367. '******************************************************************
  368. LOCAL MODE
  369. LOCAL FN AEGetAttributePtr(theAEvent&,keyWord&,desiredType&,returnType&,bufferPtr&,bufferSize&,actualSize&)
  370.   `     clr.w   -(sp)
  371.   `     move.l  ^theAEvent&,-(sp)
  372.   `     move.l  ^keyWord&,-(sp)
  373.   `     move.l  ^desiredType&,-(sp)
  374.   `     move.l  ^returnType&,-(sp)
  375.   `     move.l  ^bufferPtr&,-(sp)
  376.   `     move.l  ^bufferSize&,-(sp)
  377.   `     move.l  ^actualSize&,-(sp)
  378.   `     move.w  #$0E15,D0
  379.   `     dc.w    $A816
  380.   `     move.w  (sp)+,D0
  381.   `     ext.l   D0
  382. END FN
  383.  
  384.  
  385. '**************************************************************
  386.  
  387. LOCAL FN InitWindow
  388.   DIM wRect.8                             '8 bytes for window rectangle
  389.   DIM bRect.8                             '8 bytes for CDEF rectangle and button
  390.   DIM eRect.8                             '8 bytes for edit rect
  391.   LONG IF NOT WINDOW(-1)                  'See if already open
  392.     CALL SETRECT(bRect,50,10,0,0)         'Nope, so set rect for CDEF
  393.     CALL SETRECT(wRect,100,100,400,220)   'Set rect for window
  394.     CALL SETRECT(eRect,50,50,275,62)      'rectangle for edit field
  395.     WINDOW #_kMainWindow,"AESenderFreiBerlin",@wRect,_docNoGrow'Plain-wrap window
  396.     BUTTON #_Kpopup,_CDEF,"Send ",@bRect,81+0+1+2'CDEF
  397.     EDIT FIELD #_kEditField,"",@eRect,_framedNoCR'Build field
  398.     CALL SETRECT(bRect,50,70,120,90)      'regular button
  399.     BUTTON #_sendBtn,_enable,"Send...",@bRect,_push
  400.   END IF                                  'Done
  401. END FN                                    'bye...
  402.  
  403. '******************************************************************
  404.  
  405. LOCAL FN InitMenus
  406.   MENU _fileMenu,0,_enable,"File"
  407.   MENU _fileMenu,_senderItem,_enable,"/SSender..."
  408.   MENU _fileMenu,_quitItem,_enable,"/QQuit"
  409. END FN
  410.  
  411. '******************************************************************
  412.  
  413. '**************************************************************
  414. '*
  415. '*      AEOpenApplication
  416. '*
  417. '**************************************************************
  418. "AEOpenApp"
  419. ENTERPROC(procAEvtPtr&,procAEvtReplyPtr&,procAEvtRefCon&)
  420.   FN InitWindow
  421.   EDIT FIELD #_kEditField,"AEOpenApplication"
  422. EXITPROC
  423.  
  424. '**************************************************************
  425. '*
  426. '*      AEQuitApplication
  427. '*
  428. '**************************************************************
  429. "AEQuitApp"
  430. ENTERPROC(procAEvtPtr&,procAEvtReplyPtr&,procAEvtRefCon&)
  431.   EDIT FIELD #_kEditField,"AEQuitApplication"
  432. EXITPROC
  433.  
  434. '**************************************************************
  435. '*
  436. '*      AEPrintDocuments
  437. '*
  438. '**************************************************************
  439. "AEPrintDocs"
  440. ENTERPROC(procAEvtPtr&,procAEvtReplyPtr&,procAEvtRefCon&)
  441.   EDIT FIELD #_kEditField,"AEPrintDocuments"
  442. EXITPROC
  443.  
  444. '**************************************************************
  445. '*
  446. '*      AEOpenDocuments
  447. '*
  448. '**************************************************************
  449. "AEOpenDocs"
  450. ENTERPROC(procAEvtPtr&,procAEvtReplyPtr&,procAEvtRefCon&)
  451.   EDIT FIELD #_kEditField,"AEOpenDocuments"
  452. EXITPROC
  453.  
  454. '**************************************************************
  455. '*
  456. '*      "handleCustomEvent"
  457. '*
  458. '*      Here is where you would handle a custom event
  459. '*
  460. '**************************************************************
  461. "handleCustomEvent"
  462. ENTERPROC (procAEvtPtr&,procAEvtReplyPtr&,procAEvtRefCon&)
  463.   'Do what ever you need to do to handle a Apple event here
  464.   result% = _noErr
  465. EXITPROC = result%
  466.  
  467. '*************************************************************
  468. '*
  469. '*      FN InstallAEvents
  470. '*
  471. '*      Tries to install the event handlers. If any error
  472. '*      occurs, the program simply ends.
  473. '*
  474. '**************************************************************
  475. LOCAL FN InstallAEvents
  476.   OSErr = _false
  477.   LONG IF SYSTEM(_sysVers) > 699
  478.     LONG IF FN GESTALT(_gestaltAppleEventsAttr)
  479.       &EVENT -8,3
  480.       gAEEventProcPtr& = LINE"AEOpenApp"
  481.       OSErr = FN AEINSTALLEVENTHANDLER(_kCoreEventClass, _kAEOpenApplication,gAEEventProcPtr&,_false,_false)
  482.       LONG IF OSErr = _noErr
  483.         gAEEventProcPtr& = LINE"AEQuitApp"
  484.         OSErr = FN AEINSTALLEVENTHANDLER(_kCoreEventClass, _kAEQuitApplication,gAEEventProcPtr&,_false,_false)
  485.         LONG IF OSErr = _noErr
  486.           gAEEventProcPtr& = LINE"AEOpenDocs"
  487.           OSErr = FN AEINSTALLEVENTHANDLER(_kCoreEventClass, _kAEOpenDocuments,gAEEventProcPtr&,_false,_false)
  488.           LONG IF OSErr = _noErr
  489.             gAEEventProcPtr& = LINE"AEPrintDocs"
  490.             OSErr = FN AEINSTALLEVENTHANDLER(_kCoreEventClass, _kAEPrintDocuments,gAEEventProcPtr&,_false,_false)
  491.             LONG IF OSErr = _noErr
  492.               gAEEventProcPtr& = LINE"handleCustomEvent"
  493.               err = FN AEINSTALLEVENTHANDLER(_AECustomClass,_AECustomEventID,gAEEventProcPtr&,_false,_false)
  494.             END IF
  495.           END IF
  496.         END IF
  497.       END IF
  498.     END IF
  499.   END IF
  500.   IF OSErr <> _noErr gDone = _true        'If error just quit
  501. END FN
  502.  
  503. '******************************************************************
  504.  
  505. LOCAL FN DoMenu                           'Menu thing occured
  506.   DIM menuID                              'which menu selected
  507.   DIM itemID                              'which item selected
  508.   menuID = MENU(_menuID) : itemID = MENU(itemID)'What's up?
  509.   SELECT menuID                           'Which menu?
  510.     CASE _fileMenu                        'Our one-n-only menu
  511.       SELECT itemID                       'Which item?
  512.         CASE _senderItem                  'New window?
  513.           FN InitWindow                   'Yep, so build if
  514.         CASE _quitItem                    'Say it ain't so, Joe...
  515.           gDone = _true                   'Sorry, is so...
  516.       END SELECT                          'of itemID
  517.   END SELECT                              'of menuID
  518.   MENU                                    'Toggle highlite
  519. END FN                                    'Done
  520.  
  521.  
  522. '***************************************
  523. '*
  524. '*      FN CheckError(err)
  525. '*
  526. '*      Cheeper than dirt error handler...
  527. '*
  528. '******************************************
  529. LOCAL FN CheckError(theErr)               'See if error occured at calling routine
  530.   DIM whichErr$                           'Space for string
  531.   DIM junk                                'Don't care about button hit this time around
  532.   LONG IF theErr <> _noErr                'Oops. Non-zero if error
  533.     whichErr$ = STR$(theErr)              'Convert error to string
  534.     CALL PARAMTEXT(whichErr$,"","","")    'Shove it into alert
  535.     junk = FN ALERT(_kAEBarfAlert,_nil)   'Notify user dude
  536.   END IF                                  'Done
  537. END FN = theErr                           'Return error to caller
  538.  
  539. '****************************************************
  540. '*
  541. '*      FN SendRequiredEvent(whichEvent&)
  542. '*
  543. '*      Sends the appropriate AppleEvent to the
  544. '*      the target
  545. '*
  546. '******************************************************
  547.  
  548. LOCAL FN SendRequiredEvent(whichEvent&)
  549.   DIM AEDescRec.AEDesc                    'Descriptor record
  550.   DIM theAppleEvent.appleEvent            'New Apple event
  551.   DIM theReply.appleEvent                 'Reply Apple event
  552.   
  553.   LONG IF FN CheckError(FN AECreateDesc(_typeApplSignature,@appSignature&,_sizeOfSign,@AEDescRec)) = _noErr
  554.     LONG IF FN CheckError(FN AECreateAppleEvent(_kCoreEventClass,whichEvent&,@AEDescRec,_kAutoGenerateReturnID,1,@theAppleEvent)) = _noErr
  555.       LONG IF FN CheckError(FN AESend(@theAppleEvent,@theReply,_kAECanInteract+_kAENoReply+_kAECanSwitchLayer,_kAENormalPriority,_kAEDefaultTimeout,_nil,_nil)) = _noErr
  556.         
  557.         err = FN AEDISPOSEDESC(#@AEDescRec)'dump it
  558.         err = FN AEDISPOSEDESC(#@theAppleEvent)'dump it
  559.         err = FN AEDISPOSEDESC(#@theReply)'dump it
  560.         
  561.       END IF                              '
  562.     END IF                                '
  563.   END IF                                  '
  564. END FN                                    'done
  565.  
  566.  
  567. '******************************************************************
  568. '*
  569. '*      "idleProcedure"
  570. '*
  571. '*      A rather simple idle procedure for AESend
  572. '*
  573. '******************************************************************
  574.  
  575. "idleProcedure"
  576. ENTERPROC(theEvent&,sleepTime&,mouseRgn&)
  577.   
  578.   _cmdPeriod = 8421376                    'Command-Period
  579.   evnt% = {theEvent&}                     'Get event
  580.   
  581.   DIM RECORD keyboard                     'Map of keyboard
  582.     DIM r1&,r2&,r3&,r4&                   'Rows 1-4
  583.   DIM END RECORD 
  584.   
  585.   result% = _false                        'Assume ok
  586.   
  587.   CALL GETKEYS(keyBoard)                  'Get keyboard
  588.   
  589.   LONG IF r2& = _cmdPeriod
  590.     result& = _true                       'Command period so cancel
  591.   XELSE
  592.     SELECT evnt%                          'Ok so do events
  593.       CASE _nullEvt
  594.         sleepTime& = 15                   'set tick count
  595.         mouseRgn& = _nil                  'Mouse region
  596.       CASE _OSEvt                         'OS Event
  597.         'FN DoOSEvent                     'Handle it
  598.       CASE _activateEvt                   'Activate event
  599.         'FN DoActivateEvent               'Handle it
  600.       CASE _updatEvt                      'Update event                             
  601.         'FN DoUpdateEvent                 'Handle it
  602.     END SELECT                            '
  603.   END IF                                  '
  604. EXITPROC = result%
  605.  
  606. '******************************************************************
  607. '*
  608. '*      Here's the good stuff....  This function expects a text
  609. '*      string which it then packs into an Apple event and sends it 
  610. '*      to the server. See discussion in text for the details.
  611. '*
  612. '******************************************************************
  613. LOCAL FN sendCustomMessage(theMessage$)
  614.   DIM newAEvent.appleEvent                'A new Apple event
  615.   DIM reply.appleEvent                    'A reply Apple event
  616.   DIM newAEDesc.AEDesc                    'New descriptor record
  617.   
  618.   DIM theText$                            'buffer for the text returned from the server
  619.   DIM t&,sz&                              'work buffers
  620.   OSErr = _noErr
  621.   LONG IF FN CheckError(FN AECreateDesc(_typeApplSignature,@appSignature&,4,@newAEDesc)) = _noErr
  622.     LONG IF FN CheckError(FN AECreateAppleEvent(_AECustomClass,_AECustomEventID,@newAEDesc,_kAutoGenerateReturnID,1,@newAEvent)) = _noErr
  623.       LONG IF FN CheckError(FN AEPutParamPtr(@newAEvent,_keyDirectObject,_typeChar,@theMessage$+1,LEN(theMessage$))) = _noErr
  624.         LONG IF FN CheckError(FN AESend(@newAEvent,@reply,_kAECanInteract+_kAEWaitReply,_kAENormalPriority,120,LINE"idleProcedure",_nil)) = _noErr
  625.           
  626.           'If got here, get returned text. Specifiy buffers for text and length.
  627.           
  628.           OSErr = FN AEGetParamPtr(@reply,_keyDirectObject,_typeChar,@t&,@theText$+1,255,@sz&)
  629.           
  630.           LONG IF OSErr = _noErr
  631.             POKE @theText$,sz&            'Poke length into first byte of buffer
  632.             EDIT FIELD #_kEditField,theText$'Shove into edit field
  633.           XELSE
  634.             e$ = STR$(OSErr)              'Barfed, so show what happened
  635.             EDIT FIELD #_kEditField,e$
  636.           END IF
  637.           
  638.           'Now, check for an error at the server. You would handle the error
  639.           'the way you want afterwards
  640.           
  641.           OSErr = FN AEGetParamPtr(@reply,_keyErrorNumber,_typeLongInteger,@t&,@err&,4,@sz&)
  642.           
  643.         END IF
  644.       END IF
  645.     END IF
  646.   END IF
  647.   
  648.   'Dispose of all the records
  649.   
  650.   OSErr = FN AEDISPOSEDESC(#@reply)
  651.   OSErr = FN AEDISPOSEDESC(#@newAEDesc)
  652.   OSErr = FN AEDISPOSEDESC(#@newAEvent)
  653.   
  654. END FN
  655.  
  656. '*************************************************
  657. '*
  658. '*      Handle the Popup CDEF here
  659. '*
  660. '*************************************************
  661.  
  662. LOCAL FN DoPopUp(btnValue)
  663.   DIM dontCare                            'Space for button from alert
  664.   SELECT btnValue                         'Which popup item?
  665.     CASE _kAEOpenApp                      'AEOpenApplication
  666.       FN SendRequiredEvent(_kAEOpenApplication)'
  667.     CASE _kAEOpenDoc                      'AEOpenDocuments
  668.       FN SendRequiredEvent(_kAEOpenDocuments)'
  669.     CASE _kAEQuitApp                      'AEQuitApplication
  670.       dontCare = FN ALERT(_kScareUserAlert,0)'Tell user we're done
  671.       FN SendRequiredEvent(_kAEQuitApplication)'
  672.     CASE _kAEPrintDoc                     'AEPrintDocuments
  673.       FN SendRequiredEvent(_kAEPrintDocuments)'
  674.   END SELECT                              'of select
  675. END FN                                    'done
  676.  
  677. '*********************************************
  678. '*
  679. '*      Handle the window controls here
  680. '*
  681. '*********************************************
  682. LOCAL FN DoDialog
  683.   DIM evnt                                'Space for event
  684.   DIM id                                  'Space for dialog ID
  685.   _evnt = 0
  686.   evnt  = DIALOG(_evnt)                   'Get event
  687.   id    = DIALOG(evnt)                    'Get id
  688.   SELECT evnt                             'Which event?
  689.     CASE _btnClick                        'Button twiddled?
  690.       SELECT id                           'Pick a card, any card
  691.         CASE _AEPopup                     'Pop Tart?
  692.           FN DoPopUp(BUTTON(_kPopup))     'Yep, so dispatch CDEF item
  693.         CASE _sendBtn                     'Send text button?
  694.           theText$ = EDIT$(_kEditField)   'Grab text
  695.           FN sendCustomMessage(theText$)  'Fire One!
  696.       END SELECT                          '
  697.     CASE _wndClose                        'In window close box?
  698.       WINDOW CLOSE #_kMainWindow          'Close window
  699.   END SELECT                              'of evnt
  700. END FN                                    'done
  701.  
  702. '*********************************************
  703. '* 
  704. '*      FN Check4AE
  705. '*
  706. '*      Checks to see if an apple event occured
  707. '*      and sends it to AEPROCESSAPPLEEVENT if so.
  708. '*      Otherwise, it's a dialog or menu event, and
  709. '*      those will be processed at their handlers.
  710. '*
  711. '**********************************************    
  712. LOCAL FN Check4AE
  713.   DIM evnt                                'Space for event ID
  714.   DIM e$                                  'Prompt string for PARAMTEXT
  715.   DIM btn                                 'Button hit in alert
  716.   evnt = {EVENT}                          'First word is event ID
  717.   LONG IF evnt = _kHighLevelEvent         'Apple event?
  718.     LONG IF FN CheckError(FN AEPROCESSAPPLEEVENT(EVENT)) <> _noErr'Yep
  719.       CALL PARAMTEXT("Could not process AppleEvent","","","")'Pass prompt for alert
  720.       btn = FN ALERT(_kAEBarfAlert,_nil)  'Notify program user
  721.     END IF                                '
  722.   END IF                                  '
  723. END FN                                    'done
  724.  
  725. '***************************************
  726.  
  727. "main"
  728.  
  729. gDone = _false                            'Do until done
  730.  
  731. FN InitMenus                              'Build menu
  732. FN InitWindow                             'Build window
  733. FN InstallAEvents                         'Install AEHandlers
  734.  
  735. ON MENU FN DoMenu                         'If MENU 
  736. ON DIALOG FN DoDialog                     'If DIALOG
  737. ON EVENT FN Check4AE                      'If EVENT
  738.  
  739. DO
  740.   HANDLEEVENTS
  741. UNTIL gDone
  742. END
  743.